package edu.hut.bookshop.controller;


import com.github.pagehelper.PageInfo;
import edu.hut.bookshop.pojo.Book;
import edu.hut.bookshop.pojo.Favorites;
import edu.hut.bookshop.service.FavoritesService;
import edu.hut.bookshop.util.ResultCode;
import edu.hut.bookshop.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收藏夹的控制层
 */
@Controller
@RequestMapping("/fav")
public class FavoritesController {
    @Autowired
    private FavoritesService favoritesService;

    @ResponseBody
    @PostMapping("/insert")
    public ResultVO insertFav(Integer userId,Integer jiajuId){
        int rows = favoritesService.insertFav(userId,jiajuId);
        return new ResultVO(ResultCode.SUCCESS,null);
    }

    @ResponseBody
    @GetMapping("/selectFav")
    public ResultVO selectByUserId(Integer userId){
        List<Book> jiajus = favoritesService.selectByUserId(userId);
        return new ResultVO(ResultCode.SUCCESS,jiajus);
    }
    @ResponseBody
    @GetMapping("/deleteFav")
    public ResultVO deleteFav(Integer jiajuId){
        int rows = favoritesService.deleteFav(jiajuId);
        return new ResultVO(ResultCode.SUCCESS,null);
    }

    @RequestMapping(value = "/{userId}")
    public String favView(@PathVariable("userId") Integer userId){
        return "fav";
    }


    @GetMapping("/search")
    @ResponseBody
    public ResultVO searchFav(@RequestParam(required = true) String bookName){
        List<Book> books = favoritesService.fuzzyQueryByName(1,10,bookName);
        PageInfo pageInfo = new PageInfo(books);
        return new ResultVO(ResultCode.SUCCESS, (int) pageInfo.getTotal(), books);
    }
}
